package com.moyingren.springcloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//@Configuration
//@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Resource
//    private UserDetailsService userDetailsService;
    private UserDetailsServicesBean userDetailsService;


    /**
     * 配置认证管理器
     * */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 配置 userDetailsService ,这是用来查询用户信息的
        auth.userDetailsService(userDetailsService);
        // 设置擦除密码，如果设置成false，那么在spring security中流转的用户认证信息都会带有密码，一般我们都会擦除
        auth.eraseCredentials(true);
        super.configure(auth);
    }

//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        super.configure(web);
////        web.csrf().disable();
//    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.csrf().disable();
//                .authorizeRequests().anyRequest().permitAll();
    }

//    @Override
//    public UserDetailsService userDetailsServiceBean() throws Exception {
////        return super.userDetailsServiceBean();
//        UserDetails userDetails = User.withUsername("admin")
//                .password("$2a$10$KeCXZJo33vZvmhYIZJEx2e98qsd6uXT6WdrEz8mVXbIliEmSxaDUG")
//                .build();
//        List list = new ArrayList<UserDetails>();
//        list.add(userDetails);
//        return new InMemoryUserDetailsManager(list);
//    }
//
//    @Override
//    protected UserDetailsService userDetailsService() {
//
//        UserDetails userDetails = User.withUsername("admin")
//                .password("$2a$10$KeCXZJo33vZvmhYIZJEx2e98qsd6uXT6WdrEz8mVXbIliEmSxaDUG")
//                .build();
//        List list = new ArrayList<UserDetails>();
//        list.add(userDetails);
//        return new InMemoryUserDetailsManager(list);
//    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
